<?php
    class EnergyclassController extends GTL_Action {
         public function init() {
            parent::init();
            /* Initialize action controller here */
           
        }
        public function indexAction() {
            $this->_helper->redirector('list', 'energyclass','default');
        }

        public function viewAction() {
            if ($this->_getParam('energy_cls_id') && $this->_getParam('energy_cls_id') != '') {
                $energy_cls_id = $this->_getParam('energy_cls_id');
            } else {
                $this->_helper->redirector('list', 'energyclass', 'default');
            }
            $energyclassModel = new Model_Energyclass();
            $energyclassalldata = $energyclassModel->fetchEntryById($energy_cls_id);
            $this->view->energyclassalldata = $energyclassalldata;
        }
        public function listAction() {
            $errorMessage= Array();
            $successmessage = '';
            $where = '';
            $searchtype = '';
            $orderField = 'energy_cls_id';
            $sort = 'asc';


            /* Pagination Login */
            $itemsPerPageReviews = $this->config->PAGING_PER_PAGE;
            $currentPageReviews = 1;
            //PAGING_PER_PAGE
            if ($this->_getParam('page') && $this->_getParam('page') != '') {
                $currentPageReviews = $this->_getParam('page');
            }

            if ($this->_getParam('field') && $this->_getParam('field') != '') {
                $orderField = $this->_getParam('field');
            }

            if ($this->_getParam('sort') && $this->_getParam('sort') != '') {
                $sort = $this->_getParam('sort');
            }
            $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;

            if ($this->_request->isPost()) {
                $postedData = $this->_request->getPost();
                //Multiple Delete Logic 
                if (isset($postedData['multiaction']) && ($postedData['multiaction'] != '')) {
                    $action = trim($postedData ['multiaction']);
                    if (isset($postedData['multicheck']) && count($postedData['multicheck']) > 0) {
                        $ids = implode(',', $_POST['multicheck']);
                        $result = $this->deleteAction($_POST['multicheck']);
                    } else {
                        $errorMessage[] = ' Please select atleast one checkbox! ';
                    }
                }
                
                
            } 

            $energyclassModel = new Model_Energyclass();
            $energyclasssData = $energyclassModel->fetchLISTEnergyclass($where, $itemsPerPageReviews, $offset, $orderField, $sort);
            $paginator = new GTL_Paginator();
            $paginator->setItemsPerPage($itemsPerPageReviews);
            $paginator->setItemsTotal($energyclassModel->getEnergyclassCount($where));
            $paginator->setCurrentPage($currentPageReviews);
            $link = $this->view->url(array('controller' => 'energyclass', 'action' => 'list', 'page' => 'PAGENO'), 'default', false);
            $paginator->paginate($link);

            $this->view->paginator = $paginator;
            $this->view->energyclasssData = $energyclasssData;
            $this->view->errorMessage = $errorMessage;
            $this->view->successmessage = $successmessage;
            $this->view->statusArray = $energyclassModel->getStatusArray();
            $this->view->searchtype = $searchtype;
        }public function deleteAction($ids = Array()) {
            //Load energyclass Model 
            $energyclassModel = new Model_Energyclass();
            if ($this->_getParam('energy_cls_id') && $this->_getParam('energy_cls_id') != '') {
                $energy_cls_id = $this->_getParam('energy_cls_id');
                $energyclassModel->deleteEnergyclass($energy_cls_id);
                $this->addSuccessMessage('Record Successfully Deleted');
               $this->_helper->redirector('list', 'energyclass', 'default');
            } else {
                $energyclassModel->deleteEnergyclass($ids);
               $this->addSuccessMessage('Record Successfully Deleted');
                $this->_helper->redirector('list', 'energyclass', 'default');
            }
        }
     public function formAction() {
     //Initialize
            $successmessage = '';
            $errorMessageArray = Array();
            $data = array();
            $energyclassData = Array();
            $this->view->mode = 'Add';

            //Load energyclass Model
            $energyclassModel = new Model_Energyclass();

            //Load energyclass Form
            $energyclassForm = new Form_EnergyclassForm();

            //Load the status array for the select box in form 
            $statusArray = $energyclassModel->getStatusArray();
            $energyclassForm->energy_cls_status->addMultiOptions($statusArray);

            //Check for the energyclass Parameter
            $energyclassData['energy_cls_id'] = $this->_request->getParam('energy_cls_id');
            $energy_cls_id = (!empty($energyclassData['energy_cls_id'])) ? $energyclassData['energy_cls_id'] : null;
            if (!empty($energy_cls_id)) {
            //Set the Mode to Edit
            $this->view->mode = 'Edit';
            //Fetch data to be loaded in form 
            $data = $energyclassModel->fetchEntryById($energy_cls_id);
            }
            //Populate form 
            $this->view->form = $energyclassForm->populate($data);
            if ($this->getRequest()->isPost()) {
                //Check for Errors
                $postedData = $this->_request->getPost();
                if (!$energyclassForm->isValid($postedData)) {
                    $errorMessage = $energyclassForm->getMessages();
                    foreach ($errorMessage as $_err) {
                        foreach ($_err as $_val) {
                            $errorMessageArray[] = $_val;
                        }
                    }
                } else {
                
                        
                    if (count($errorMessageArray) > 0) {

                    } else {
                    
                          
                        if ($this->view->mode == 'Add') {
                            $energyclassData['energy_cls_createddate'] = date('Y-m-d H:i:s');
                        }
                        //energyclass save
                        $energyclassData['energy_cls_id'] = $energy_cls_id;
                        $energyclassData['energy_cls_name'] = $postedData['energy_cls_name'];
						$energyclassData['energy_cls_updateddate'] = date('Y-m-d H:i:s');
						$energyclassData['energy_cls_status'] = $postedData['energy_cls_status'];
						

                        $energyclassModel->save($energyclassData, 'energy_cls_id');
                        if ($this->view->mode == 'Add') {
                            $successmessage = 'Energyclass add successfully';
                        } else {
                            $successmessage = 'Energyclass updated successfully';
                        }
                        $this->addSuccessMessage($successmessage);
                        $this->_helper->redirector('list', 'energyclass', 'default');
                    }
                }
            }
            $this->view->energyclassid = $energy_cls_id;
            $this->view->errorMessageArray = $errorMessageArray;
            $this->view->succesMessage = $successmessage;
            $this->view->form = $energyclassForm;


    }
    }?>